package com.zt.bigdata.practise.javastrategy.chapter1;

import com.zt.bigdata.practise.javastrategy.chapter1.dto.Person;
import com.zt.bigdata.practise.javastrategy.chapter1.myInterface.MessageSend;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 */
public class Chapter1 {
    public static void main(String[] args) {
        Chapter1 chapter1 = new Chapter1();
        //1.1 lambda表达式
        File directory = new File("/Users/zhangtong/IdeaProjects/hangy/zhangtong/springcloud/allin/test/practise/src/main/java/com/zt/bigdata/practise");
        String[] filenames = directory.list((dir, name) -> name.endsWith(".java"));
        System.out.println(Arrays.asList(filenames));


        //1.2 方法引用
        Stream.of(3, 1, 4, 1, 5, 9)
                .forEach(x -> System.out.print(x + " "));
        System.out.println();
        Stream.of(3, 1, 4, 1, 5, 9)
                .forEach(System.out::print);
        System.out.println();
        Stream.generate(Math::random).limit(10).forEach(System.out::print);
        System.out.println();

        List<String> strings =
                Arrays.asList("this", "is", "a", "list", "of", "strings");


        //sorted(内部为比较的依据  如根据string s 本身比较  如根据 对象的某个字段 Comparator.comparing(Human::getName) )
        strings.stream().sorted(Comparator.comparing(s -> s)).collect(Collectors.toList());
        strings.stream().sorted(String::compareTo);

        Stream.of("this", "is", "a", "list", "of", "strings").map(String::length);
        //1.3 构造函数引用
        List<String> names =
                Arrays.asList("Grace Hopper", "Barbara Liskov", "Ada Lovelace",
                        "Karen Spärck Jones");
        List<Person> people = names.stream().map(Person::new).collect(Collectors.toList());
        List<Person> people1 = names.stream().map(name -> name.split(" ")).map(Person::new).collect(Collectors.toList());
        System.out.println(people1);
        Person[] people2 = names.stream().map(Person::new).toArray(Person[]::new);

        //1.4 函数式接口  MessageSend
        //发送钉钉消息
        execute("dingding", s -> System.out.println("发送 " + s));
        execute(people2[0], s -> System.out.println("调用短信接口发数据: " + s));

        //接口中的静态方法
        MessageSend.myStaticMethod();
        strings.stream().sorted(
                //接口中的静态方法
                Comparator.reverseOrder()
        ).collect(
                //接口中的静态方法
                Collectors.toList()
        );
    }

    public static <T> void execute(T s, MessageSend<T> messageSend) {
        System.out.println("开始发送数据");
        messageSend.send(s);
        System.out.println("开始发送完成");
    }
}

